package com.dongdongshop.aop;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

//提取打印日志
//此注解表明是个切面类
@Aspect
@Component
public class Logaop {

    private Logger logger= LoggerFactory.getLogger(Logaop.class);

    //@Pointcut:切点注解
    //要切的有service下面的这些类，或者带有@Dklog这个注解的类
    @Pointcut("execution(public * com.dongdongshop.service.*.*(..))|| @annotation(com.dongdongshop.log.Dklog)")
    public void pt(){

    }

    @Before("pt()")//前置通知
    public void dobefore(JoinPoint joinPoint) throws Throwable{
        Signature signatrue=joinPoint.getSignature();//被调用的方法
        logger.info("正在执行的方法是：{}" ,signatrue);
        Object[]args= joinPoint.getArgs();
        if(args==null){
            logger.info("此方法没有参数");
        }else{
            logger.info("参数为：{}", args);
        }
    }

    @AfterReturning(pointcut ="pt()",returning="obj")
    public void doAfterReturn(Object obj)throws Throwable{
        //打印返回值
        logger.info("方法执行成功，返回值：{}", JSONObject.toJSONString(obj));
    }





}
